//
//  KlineGraphView.m
//  SHLineGraphView
//
//  Created by 吕飞俊 on 15/11/3.
//  Copyright © 2015年 grevolution. All rights reserved.
//

#import "LvfjKlineGraphView.h"
#import "SHLineGraphView.h"
#import "SHPlot.h"

@implementation LvfjKlineGraphView

-(instancetype)initWithFrame:(CGRect)frame
                   dataArray:(NSArray *)dataArray
                         dic:(NSDictionary *)dic{
    if (self = [super initWithFrame:frame]) {
        
        //initate the graph view
        SHLineGraphView *_lineGraph = [[SHLineGraphView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        [self drawLable:_lineGraph dic:dic];
        
        for (NSInteger i = 0; i < dataArray.count; i++) {
            _lineGraph.maxHeight = [dic[@"maxY"] floatValue];
            _lineGraph.horizontalLineNo = 10;
            _lineGraph.btRadius = 5;
            [self drawLine:_lineGraph dataDic:[dataArray objectAtIndex:i]];
        }
        
        
        //You can as much `SHPlots` as you can in a `SHLineGraphView`
        [_lineGraph setupTheView];//画图
        
        [self addSubview:_lineGraph];
        
    }
    return self;
}

/**设置左右lable*/
-(void)drawLable:(SHLineGraphView *)_lineGraph
             dic:(NSDictionary *)dic{
    //set the main graph area theme attributes
    
    /**
     *  theme attributes dictionary. you can specify graph theme releated attributes in this dictionary. if this property is
     *  nil, then a default theme setting is applied to the graph.
     */
    NSDictionary *_themeAttributes = @{
                                       kXAxisLabelColorKey : [UIColor colorWithRed:0.48 green:0.48 blue:0.49 alpha:0.4],
                                       kXAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:10],
                                       kYAxisLabelColorKey : [UIColor colorWithRed:0.48 green:0.48 blue:0.49 alpha:0.4],
                                       kYAxisLabelFontKey : [UIFont fontWithName:@"TrebuchetMS" size:10],
                                       kYAxisLabelSideMarginsKey : @20,//y lable的起始坐标
                                       kPlotBackgroundLineColorKye : dic[@"horizontalLineColor"]//横线颜色
                                       };
    _lineGraph.themeAttributes = _themeAttributes;
}

/**设置点线面*/
-(void)drawLine:(SHLineGraphView *)_lineGraph
        dataDic:(NSDictionary *)dataDic{
    SHPlot *_plot = [[SHPlot alloc] init];
    
    //set the plot attributes
    NSArray *array = dataDic[@"values"];
    
    //线的数值
    NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic =[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:[array[i] doubleValue]]
                                                       forKey:[NSNumber numberWithInt:i]];
        [mutableArray addObject:dic];
    }
    _plot.plottingValues = mutableArray;
    //点击按钮显示的字符数组
    _plot.plottingPointsLabels = array;
    
    //点线面
    NSDictionary *_plotThemeAttributes = @{
                                           kPlotFillColorKey : dataDic[@"areColor"],//面
                                           kPlotStrokeWidthKey : @2,//线的宽度或点的直径
                                           kPlotStrokeColorKey : dataDic[@"lineColor"],//线
                                           kPlotPointFillColorKey : dataDic[@"pointColor"],//点
                                           kPlotPointValueFontKey : [UIFont fontWithName:@"TrebuchetMS" size:18]
                                           };
    
    _plot.plotThemeAttributes = _plotThemeAttributes;
    [_lineGraph addPlot:_plot];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
